package com.incar.chuxing.utils;

import android.support.annotation.NonNull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

public class UniqueQueue<T> implements Queue<T> {
    private final Queue<T> queue = new LinkedList<T>();
    /**
     * Set集合用来去重
     **/
    private final Set<T> set = new HashSet<T>();
    private ReentrantLock lock = new ReentrantLock();


    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @NonNull
    @Override
    public Iterator<T> iterator() {
        return null;
    }

    @NonNull
    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @NonNull
    @Override
    public <T1> T1[] toArray(@NonNull T1[] a) {
        return null;
    }

    @Override
    public boolean add(T t) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(@NonNull Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(@NonNull Collection<? extends T> c) {
        return false;
    }

    @Override
    public boolean removeAll(@NonNull Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(@NonNull Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public boolean offer(T t) {
        // 首先进行锁定操作
        lock.lock();
        try {
            // 直接调用Set的add方法，如果当前集合已经存在此对象，则方法会返回失败
            if (set.add(t)) {
                // 否则将对象加入到队列中
                queue.add(t);
                return true;
            } else {
                return false;
            }
        } finally {
            // 进行锁释放操作
            lock.unlock();
        }
    }

    @Override
    public T remove() {
        return null;
    }


    @Override
    public T poll() {
        lock.lock();
        try {
            // 从队列首部取出对象
            T t = queue.poll();
            // 如果对象不为空，则从Set集合中相应的移出
            if (t != null) {
                set.remove(t);
            }
            return t;
        } finally {
            lock.unlock();
        }
    }

    @Override
    public T element() {
        return null;
    }

    @Override
    public T peek() {
        return null;
    }

}